// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: donggu

#include <cstdio>

#include <docopt/docopt.h>

#include "coin2/base/time.h"
#include "coin2/exchange/base/symbology/product.h"
#include "coin2/exchange/factory/symbology/product.h"

using coin2::exchange::symbology::ProductConfig;
using coin2::exchange::symbology::ProductRequest;

static const char USAGE[] = R"(
    Symbol Conversion

    Usage:
      symbol-convert <mea> (--norm=<symbol> | --native=<symbol>) [--time=<int64_or_iso8601>]

    Options:
      -h --help                      Show this screen.
      --time=<int64_or_iso8601>             int64 or iso8601

    Example:
      bazel build //cc/appcoin2/support/symbology:convert
      ./bazel-bin/vanguard/binned_feed --mea Futures.Huobi.v1 --product BTC-USD.QUARTER --feed_machine=feed-05.ap.northeast-1.aws --recipe incremental --start 2020-06-20 --duration P1D
)";

int main(int argc, char* argv[]) {
  std::map<std::string, docopt::value> args = docopt::docopt(
      USAGE,
      {argv + 1, argv + argc},
      true,     // show help if requested
      "v0.1");  // version string

  // for (const auto& [k, v] : args) {
  //   printf("%s\n", k.c_str());
  // }

  // input symbol can be 4 forms
  // relative norm
  // absolute norm
  // feed native
  // order native
  auto mea = args["<mea>"].asString();
  int64_t time = 0;

  if (args["<time>"]) {
    time = TimestampFromString(args["<time>"].asString());
  } else {
    time = GetCurrentTimestamp();
  }

  ProductConfig config;
  if (args["--norm"]) {
    config.add_norms(args["--norm"].asString());
  } else if (args["--native"]) {
    config.add_natives(args["--native"].asString());
  } else {
    printf("no --norm or --native\n");
    return 1;
  }

  auto products = ProductRequestsFromConfig(mea, config, time);
  auto& prod = products[0];

  printf("feed_native = '%s'\n", prod->feed_native_symbol().c_str());
  printf("order_native = '%s'\n", prod->order_native_symbol().c_str());
  printf("absolute_norm = '%s'\n", prod->absolute_norm().c_str());
  printf("relative_norm = '%s'\n", prod->relative_norm().c_str());

  return 0;
}
